home *** CD-ROM | disk | FTP | other *** search
- //*** Sound.lib - A few pc speaker sound routines
- //*** ver.1
- //
- //*** sound(): play a tone for specified time
- // USAGE: void sound(int frequency,int duration)
- // WHERE: frequency: sound frequency in Hz
- // duration: time to play in milliseconds
- //
- //*** StartTone(): start playing a tone
- // USAGE: void StartTone(int frequency)
- // WHERE: frequency: sound frequency in Hz
- //
- //*** StopTone(): stop playing any tone
- // USAGE void StopTone()
- //
-
- sound(pFrequency,pDuration)
- {
- StartTone(pFrequency);
- suspend(pDuration);
- StopTone();
- }
-
- StartTone(pFrequency) // start 8253 programmable timer playing this frequency
- {
- // determine counter to send to the 8253 programmable timer
- #define CHIP_RATE 1193180
- counter = CHIP_RATE / pFrequency;
- // program 8253
- #define COUNTER_REGISTER 0x42
- #define COMMAND_REGISTER 0x43
- #define SPEAKER_REGISTER 0x61
- speaker = inport(SPEAKER_REGISTER);
- if ( !(speaker & 0x3) ) {
- speaker |= 0x3;
- outport(SPEAKER_REGISTER,speaker);
- outport(COMMAND_REGISTER,0xB6);
- }
- outport(COUNTER_REGISTER,counter & 0xFF);
- outport(COUNTER_REGISTER,(counter >> 8) & 0xFF);
- }
-
- StopTone()
- {
- outport(SPEAKER_REGISTER,inport(SPEAKER_REGISTER) & 0xfc);
- }